import React, {useState} from 'react';
import {
    View,
    Text,
    TextInput,
    Button,
    StyleSheet,
    ScrollView,
    Image,
    TouchableOpacity, ToastAndroid,
} from 'react-native';
import * as ImagePicker from 'react-native-image-picker';
import Icon from 'react-native-vector-icons/FontAwesome';
import {noticeAdd, noticeUpdate} from "../../../util/api";

const NoticeUpdateScreen = (props: {navigation: any}) => {
  console.log(props);
  let notice = {};
  if (props && props.route && props.route.params) {
      notice = props.route.params.item;
      // setNotice(item);
  }
  const [score, setScore] = useState(notice.score ? notice.score : '');
  const [description, setDescription] = useState(notice.noticecontent ? notice.noticecontent : '');


  const submit = () => {
      if (!description) {
          ToastAndroid.show('请填写描述内容', 2000);
          return;
      }
      const data = {
          noticeid: notice.noticeid,
          score: score,
          noticecontent: description,
      };
      noticeUpdate(data).then((res) => {
          ToastAndroid.show('修改成功', 2000);
      }).catch((err) => {
          ToastAndroid.show('修改失败', 2000);
      }).finally(() => {
          props.navigation.goBack();
      });
  };

  return (
    <ScrollView contentContainerStyle={styles.container}>
      <Text style={styles.label}>描述内容</Text>
      <TextInput
        style={[styles.input]}
        value={description}
        onChangeText={setDescription}
        multiline
      />

      <View style={{flexDirection: 'row', alignSelf: 'center'}}>
        <View
          style={{
            width: 75,
            marginRight: 5,
          }}>
          <Button title="提交" onPress={submit} />
        </View>
        <View style={{width: 75, marginLeft: 5}}>
          <Button title="取消" onPress={() => props.navigation.goBack()} />
        </View>
      </View>
    </ScrollView>
  );
};

const styles = StyleSheet.create({
  container: {
    padding: 20,
  },
  label: {
    fontSize: 16,
    marginBottom: 5,
  },
  input: {
    borderWidth: 1,
    borderColor: 'gray',
    borderRadius: 5,
    marginBottom: 10,
    padding: 10,
  },
  button: {
    fontSize: 16,
    color: 'blue',
    marginBottom: 10,
  },
  image: {
    width: 100,
    height: 100,
    marginBottom: 10,
    margin: 2,
  },
});

export default NoticeUpdateScreen;
